package com.eastfair.home.controller;

import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.builder.ExhibitorFeign;
import com.eastfair.builder.dto.ExhibitorDTO;
import com.eastfair.builder.vo.ExhibitorVO;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.home.enumeration.ServiceProviderTypeEnum;
import com.eastfair.venuebooking.api.BondManageFeign;
import com.eastfair.venueservice.dto.BondManageDTO;
import com.eastfair.venueservice.vo.BondManageVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 搭建商 展商展位 REST API.
 *
 * @author dqq
 * @date 2022 /11/29
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/exhibitor")
public class HomeExhibitorController {

    @Resource
    private ExhibitorFeign exhibitorFeign;
    @Resource
    private BondManageFeign bondManageFeign;



    /**
     * 查询展商VO列表.
     *
     * @param id the id
     * @return the exhibitor
     */
    @ApiOperation("查询展商VO列表")
    @GetMapping("/get/{id}")
    public R<ExhibitorVO> getExhibitor(@PathVariable("id") Long id) {
        log.info("getExhibitor - 获取展商详情, id={}", id);
        try {
            return exhibitorFeign.getExhibitor(id);
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 修改参展商.
     *
     * @param exhibitorDTO the exhibitor dto
     * @return the r
     */
    @ApiOperation("修改参展商")
    @PostMapping("/update")
    public R<Boolean> updateExhibitor(@RequestBody ExhibitorDTO exhibitorDTO) {
        log.info("updateExhibitor - 修改参展商, exhibitorDTO={}", exhibitorDTO);
        try {
            if (exhibitorDTO.getShouldEarnestMoney() != null) {
                R<List<BondManageVO>> r1= bondManageFeign.getBondManageList(exhibitorDTO);
                List<BondManageVO> list = r1.getData();
                if(CollectionUtils.isNotEmpty(list)){
                    bondManageFeign.delBondManageByServiceProviderId(exhibitorDTO.getBuilderId());
                }
                BondManageDTO bondManageDTO =new BondManageDTO();
                bondManageDTO.setExhibitionManageId(exhibitorDTO.getExhibitionManageId());
                bondManageDTO.setExhibitionManageName(exhibitorDTO.getExhibitionManageName());
                bondManageDTO.setServiceProviderId(exhibitorDTO.getBuilderId());
                bondManageDTO.setServiceProviderName(exhibitorDTO.getBuilderName());
                bondManageDTO.setSpaceCode(exhibitorDTO.getSpaceCode());
                bondManageDTO.setSpaceName(exhibitorDTO.getSpaceName());
                bondManageDTO.setSiteCode(exhibitorDTO.getSiteCode());
                bondManageDTO.setSiteName(exhibitorDTO.getSiteName());
                bondManageDTO.setBondAmount(exhibitorDTO.getShouldEarnestMoney());
                bondManageDTO.setSystemScene(ContextUtil.getSystemScene());
                bondManageDTO.setServiceProviderType(ServiceProviderTypeEnum.BUILD_SERVICE_PROVIDER.getCode());
                bondManageFeign.saveBondManageInfo(bondManageDTO);
            }
            R<Boolean> r = exhibitorFeign.updateExhibitor(exhibitorDTO);
            return r;
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 展位重置.
     *
     * @param id the id
     * @return the r
     */
    @SysLog("展位重置")
    @GetMapping("/reset/{id}")
    public R<Boolean> updateIsReset(@PathVariable("id") Long id) {
        log.info("updateIsReset - 更新展位重置, id={}", id);
        try {
            return exhibitorFeign.updateIsReset(id);
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 批量移除展商.
     *
     * @param ids the ids
     * @return the r
     */
    @ApiOperation("批量移除展商")
    @PostMapping("/remove")
    public R<Boolean> removeByIds(@RequestBody List<Long> ids) {
        log.info("removeByIds - 批量移除展商, ids={}", ids);
        try {
            return exhibitorFeign.removeByIds(ids);
        } catch (BizException e) {
            return R.fail(e);
        }
    }
}